﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package pbjAS {
    import flash.display.*;
    import flash.utils.*;
    import pbjAS.params.*;
    import pbjAS.regs.*;
    import pbjAS.consts.*;
    import pbjAS.ops.*;

    public class PBJTools {

        public static function dumpReg(_arg1:PBJReg):String{
            if ((_arg1 is RInt)){
                return ((("i" + _arg1.value) + ext(_arg1.data)));
            };
            if ((_arg1 is RFloat)){
                return ((("f" + _arg1.value) + ext(_arg1.data)));
            };
            return (null);
        }
        public static function ext(_arg1:Array):String{
            var _local3:String;
            if (_arg1 == null){
                return ("");
            };
            var _local2 = ".";
            for each (_local3 in _arg1) {
                switch (_local3){
                    case PBJChannel.R:
                        _local2 = (_local2 + "r");
                        break;
                    case PBJChannel.G:
                        _local2 = (_local2 + "g");
                        break;
                    case PBJChannel.B:
                        _local2 = (_local2 + "b");
                        break;
                    case PBJChannel.A:
                        _local2 = (_local2 + "a");
                        break;
                    case PBJChannel.M2x2:
                        _local2 = (_local2 + "2x2");
                        break;
                    case PBJChannel.M3x3:
                        _local2 = (_local2 + "3x3");
                        break;
                    case PBJChannel.M4x4:
                        _local2 = (_local2 + "4x4");
                        break;
                };
            };
            return (_local2);
        }
        public static function splitShaderInputFloat4(_arg1:ShaderInput, _arg2:Object, _arg3:Number):void{
            if (_arg2.length > 16777216){
                throw (new Error("Texture length cannot exceed 16777216."));
            };
            while ((_arg2.length % 4) != 0) {
                _arg2.push(1);
            };
            var _local4:Number = (_arg2.length / 4);
            var _local5:Number = Math.sqrt(_local4);
            var _local6:Number = Math.ceil((_local4 / Math.floor(_local5)));
            var _local7:Number = Math.ceil((_local4 / _local6));
            if ((((_local6 > 0x2000)) || ((_local7 > 0x2000)))){
                throw (new Error("Texture width or height cannot exceed 8192"));
            };
            while (_arg2.length < ((_local6 * _local7) * 4)) {
                _arg2.push(1);
                _arg2.push(1);
                _arg2.push(1);
                _arg2.push(1);
            };
            _arg1.width = _local6;
            _arg1.height = _local7;
            _arg1.input = _arg2;
        }
        public static function getMatrixMaskBits(_arg1:int):int{
            switch (_arg1){
                case 1:
                    return (196);
                case 2:
                    return (232);
                case 3:
                    return (252);
                default:
                    return (-1);
            };
        }
        public static function dumpType(_arg1:String):String{
            return (_arg1);
        }
        public static function hexDump(_arg1:ByteArray):String{
            var _local2 = "";
            _arg1.position = 0;
            while (_arg1.bytesAvailable) {
                _local2 = (_local2 + (_arg1.readUnsignedByte().toString(16) + " "));
            };
            _arg1.position = 0;
            return (_local2);
        }
        public static function dumpOpCode(_arg1:PBJOpcode):String{
            var _local2:String;
            if ((_arg1 is OpNop)){
                return ("nop");
            };
            if ((_arg1 is OpSampleNearest)){
                return ((((((("sampleNearest " + dumpReg((_arg1 as OpSampleNearest).dst)) + ", t") + (_arg1 as OpSampleNearest).srcTexture) + "[") + dumpReg((_arg1 as OpSampleNearest).src)) + "]"));
            };
            if ((_arg1 is OpSampleLinear)){
                return ((((((("sampleLinear " + dumpReg((_arg1 as OpSampleLinear).dst)) + ", t") + (_arg1 as OpSampleLinear).srcTexture) + "[") + dumpReg((_arg1 as OpSampleLinear).src)) + "]"));
            };
            if ((_arg1 is OpLoadInt)){
                return (((("loadint " + dumpReg((_arg1 as OpLoadInt).reg)) + ", ") + (_arg1 as OpLoadInt).v));
            };
            if ((_arg1 is OpLoadFloat)){
                return (((("loadfloat " + dumpReg((_arg1 as OpLoadFloat).reg)) + ", ") + (_arg1 as OpLoadFloat).v));
            };
            if ((_arg1 is OpIf)){
                return (("if " + dumpReg((_arg1 as OpIf).cond)));
            };
            if ((_arg1 is OpElse)){
                return ("else");
            };
            if ((_arg1 is OpEndIf)){
                return ("endif");
            };
            _local2 = getQualifiedClassName(_arg1);
            return (((((_local2 + " ") + dumpReg((_arg1 as Object).dst)) + ", ") + dumpReg((_arg1 as Object).src)));
        }
        public static function dump(_arg1:PBJ):String{
            var _local3:PBJMeta;
            var _local4:PBJParam;
            var _local5:PBJOpcode;
            var _local6:PBJMeta;
            var _local2:String = new String();
            _local2 = (_local2 + (("version : " + _arg1.version) + "\n"));
            _local2 = (_local2 + ((("name : '" + _arg1.name) + "'") + "\n"));
            for each (_local3 in _arg1.metadatas) {
                _local2 = (_local2 + (((("  meta " + _local3.key) + " : ") + dumpValue(_local3.value)) + "\n"));
            };
            for each (_local4 in _arg1.parameters) {
                if ((_local4.parameter is Parameter)){
                    _local2 = (_local2 + (((((((("  param " + ((_local4.parameter as Parameter).out) ? "out" : "in ") + " ") + dumpReg((_local4.parameter as Parameter).reg)) + " ") + dumpType((_local4.parameter as Parameter).type)) + "\t") + _local4.name) + "\n"));
                } else {
                    if ((_local4.parameter is Texture)){
                        _local2 = (_local2 + ((((((("  text" + (_local4.parameter as Texture).index) + " ") + (_local4.parameter as Texture).channels) + "-channels") + "\t") + _local4.name) + "\n"));
                    };
                };
                for each (_local6 in _local4.metadatas) {
                    _local2 = (_local2 + (((("    meta " + _local6.key) + " : ") + dumpValue(_local6.value)) + "\n"));
                };
            };
            _local2 = (_local2 + "code :\n");
            for each (_local5 in _arg1.code) {
                _local2 = (_local2 + (("  " + dumpOpCode(_local5)) + "\n"));
            };
            return (_local2);
        }
        public static function getValueType(_arg1:PBJConst):String{
            if ((_arg1 is PFloat)){
                return (PBJType.TFloat);
            };
            if ((_arg1 is PFloat2)){
                return (PBJType.TFloat2);
            };
            if ((_arg1 is PFloat3)){
                return (PBJType.TFloat3);
            };
            if ((_arg1 is PFloat4)){
                return (PBJType.TFloat4);
            };
            if ((_arg1 is PFloat2x2)){
                return (PBJType.TFloat2x2);
            };
            if ((_arg1 is PFloat3x3)){
                return (PBJType.TFloat3x3);
            };
            if ((_arg1 is PFloat4x4)){
                return (PBJType.TFloat4x4);
            };
            if ((_arg1 is PInt)){
                return (PBJType.TInt);
            };
            if ((_arg1 is PInt2)){
                return (PBJType.TInt2);
            };
            if ((_arg1 is PInt3)){
                return (PBJType.TInt3);
            };
            if ((_arg1 is PInt4)){
                return (PBJType.TInt4);
            };
            if ((_arg1 is PString)){
                return (PBJType.TString);
            };
            return (null);
        }
        public static function splitShaderInputFloat(_arg1:ShaderInput, _arg2:Object, _arg3:Number):void{
            if (_arg2.length > 16777216){
                throw (new Error("Texture length cannot exceed 16777216."));
            };
            var _local4:Number = _arg2.length;
            var _local5:Number = Math.sqrt(_local4);
            var _local6:Number = Math.ceil((_local4 / Math.floor(_local5)));
            var _local7:Number = Math.ceil((_local4 / _local6));
            if ((((_local6 > 0x2000)) || ((_local7 > 0x2000)))){
                throw (new Error("Texture width or height cannot exceed 8192"));
            };
            while (_arg2.length < (_local6 * _local7)) {
                _arg2.push(1);
            };
            _arg1.width = _local6;
            _arg1.height = _local7;
            _arg1.input = _arg2;
        }
        public static function dumpValue(_arg1:PBJConst):String{
            if ((_arg1 is PFloat)){
                return ((_arg1 as PFloat).f.toString());
            };
            if ((_arg1 is PFloat2)){
                return (call("float2", [(_arg1 as PFloat2).f1, (_arg1 as PFloat2).f2]));
            };
            if ((_arg1 is PFloat3)){
                return (call("float3", [(_arg1 as PFloat3).f1, (_arg1 as PFloat3).f2, (_arg1 as PFloat3).f3]));
            };
            if ((_arg1 is PFloat4)){
                return (call("float4", [(_arg1 as PFloat4).f1, (_arg1 as PFloat4).f2, (_arg1 as PFloat4).f3, (_arg1 as PFloat4).f4]));
            };
            if ((_arg1 is PFloat2x2)){
                return (call("float2x2", (_arg1 as PFloat2x2).f));
            };
            if ((_arg1 is PFloat3x3)){
                return (call("float3x3", (_arg1 as PFloat3x3).f));
            };
            if ((_arg1 is PFloat4x4)){
                return (call("float4x4", (_arg1 as PFloat4x4).f));
            };
            if ((_arg1 is PInt)){
                return (new String((_arg1 as PInt).i));
            };
            if ((_arg1 is PInt2)){
                return (call("int2", [(_arg1 as PInt2).i1, (_arg1 as PInt2).i2]));
            };
            if ((_arg1 is PInt3)){
                return (call("int3", [(_arg1 as PInt3).i1, (_arg1 as PInt3).i2, (_arg1 as PInt3).i3]));
            };
            if ((_arg1 is PInt4)){
                return (call("int4", [(_arg1 as PInt4).i1, (_arg1 as PInt4).i2, (_arg1 as PInt4).i3, (_arg1 as PInt4).i4]));
            };
            if ((_arg1 is PString)){
                return ((("'" + (_arg1 as PString).s) + "'"));
            };
            return (null);
        }
        private static function call(_arg1:String, _arg2:Array):String{
            return ((((_arg1 + "(") + _arg2.join(",")) + ")"));
        }

    }
}//package pbjAS 
